#include <stdio.h>
#include <string.h>
#include "drv_led.h"
#include "drv_water.h"
#include "drv_exit.h"
#include "drv_usart.h"
#include "drv_time.h"
#include "drv_iic.h"
#include "drv_sht20.h"
#include "drv_oled.h"
#include "drv_bmp.h"
#include "drv_spi.h"
#include "drv_seg.h"
#include "drv_esp.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"


#define TASK_CONTROL_PRIORITY     2   // 语音控制任务优先级最高
#define TASK_TEMHUM_PRIORITY      1   // 测量温湿度任务次之



#define TASK_CONTROL_STACK        256
#define TASK_TEMHUM_STACK         256
#define START_STACK             128

TaskHandle_t task_control_handle;
TaskHandle_t task_temhum_handle;
TaskHandle_t start_handle;


int walk_state; // 0:停止, 1:行走中
int walk_step;
int back_state; // 0:停止, 1:行走中
int back_step;
int water_state;
extern uint8_t usart3_rx_overflow;
// 函数声明
void vTask_start(void *pvParameter);
void Task_control(void *pvParameter);
void control_function(void);
void Task_temhum(void *pvParameter);
void process_control_command(uint8_t flag);
void drv_oled_show(void);
void drv_oled_th(void);
void drv_oled_hu(void);
int main(void)
{    
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
    
    // 初始化硬件
    drv_tim6_init();
    drv_tim3_init();
    drv_sht20_init();
    drv_sht20_reset();    
    drv_water_init();
    drv_usart6_init();    // 调试串口
    drv_usart2_init();    // 语音模块串口
    drv_esp_init();       // WiFi模块初始化
    drv_led_init();
    
    //drv_led_open();    
   
    drv_led_open();
    // 创建启动任务
    xTaskCreate(vTask_start, "start_task", START_STACK, NULL, 0, &start_handle);   
    vTaskStartScheduler();        
}

void vTask_start(void *pvParameter)
{   
    // 创建控制任务
    xTaskCreate(Task_control, "control_task", TASK_CONTROL_STACK, NULL, TASK_CONTROL_PRIORITY, &task_control_handle);
    
    // 创建WiFi控制任务
    xTaskCreate(Task_temhum, "temhum_task", TASK_TEMHUM_STACK, NULL, TASK_TEMHUM_PRIORITY, &task_temhum_handle);
    
    vTaskDelete(start_handle);
}

// 控制任务
void Task_control(void *pvParameter)
{
    printf("控制任务启动\r\n");
    for(;;)
    {        
        if(usart3_rx_overflow == 1) 
        {
            usart3_command();
        }
        vTaskDelay(100); 
    } 
}

//温湿度任务
void Task_temhum(void *pvParameter)
{
    for(;;)
    {
        drv_sht20_readtemp();
        drv_sht20_readhum();    
    }
    vTaskDelay(100); 
}
void water_show(void)
{
    while(water_state)
    {
        drv_water1_open();
        vTaskDelay(500);
        drv_water1_close();
        vTaskDelay(500);
        drv_water2_open();
        vTaskDelay(500);
        drv_water2_close();
        vTaskDelay(500);
        drv_water3_open();
        vTaskDelay(500);
        drv_water3_close();
        vTaskDelay(500);
    }
    while(!water_state)
    {
        drv_water1_close();
        drv_water2_close();
        drv_water3_close();
    }
}
// 处理命令
typedef enum 
{
	temptest,
	humtest,
	walk,
	back,
	standup,
	sitdown,
    ledon,
	ledoff,
    wateron,
    wateroff,
    hello
}current_state;
void process_control_command(uint8_t flag)
{
    // 查询温度
    switch(flag)
    {    
       case temptest:    
         drv_oled_th(); 
         break;
       case humtest:    
         drv_oled_th(); 
         break;
       case walk:    
         walk_state = 1; // 0:停止, 1:行走中
         walk_step = 0;
         back_state = 0; 
         break;
       case back:    
         back_state = 1; 
         back_step = 0;
         walk_state = 0;
         break;
       case standup:    
          back_state = 0; 
          walk_state = 0; 
          drv_tim3_stand(); 
         break;
       case sitdown:    
          back_state = 0; 
          walk_state = 0; 
          drv_tim3_down(); 
         break;
       case ledon:    
          back_state = 0; 
          walk_state = 0; 
          drv_led_open(); 
         break;
       case ledoff:    
          back_state = 0; 
          walk_state = 0; 
          drv_led_close(); 
          break;
       case wateron:    
          water_state = 1; 
          break;
       case wateroff:    
          water_state = 0; 
          break;
       case hello:    
          drv_oled_show(); 
          break;
    }      
}

u8 t=' ';
void drv_oled_show(void)
{	
    OLED_ShowChinese(0,2,0,16,1);
    OLED_ShowChinese(18,2,1,16,1);
    OLED_ShowChinese(36,2,2,16,1);
    OLED_ShowChinese(54,2,3,16,1);
    OLED_ShowChinese(72,2,4,16,1);
    OLED_ShowChinese(90,2,5,16,1);
    OLED_ShowChinese(108,2,6,16,1);
    OLED_Refresh();
    drv_systick_ms(1500);
    OLED_Clear();
    OLED_ShowPicture(0,0,128,64,BMP1,1);
    OLED_Refresh();
    drv_systick_ms(1500);
    OLED_Clear();                    
    
    OLED_ShowChinese(0,2,7,16,1);
    OLED_ShowChinese(18,2,8,16,1);
    OLED_ShowChinese(36,2,9,16,1);
    OLED_ShowChinese(54,2,10,16,1);
    OLED_ShowChinese(72,2,11,16,1);
    OLED_ShowChinese(90,2,12,16,1);
    OLED_ShowChinese(108,2,13,16,1);
    OLED_Refresh();
    drv_systick_ms(1500);
    OLED_Clear();
}
void drv_oled_th(void)
{
    u32 r = 0;            
    r = (u32)(drv_sht20_gettemp() * 100);
    OLED_ShowFloat(50, 20 , r, 2, 16, 1);	
    OLED_Refresh();
    drv_systick_ms(2500);
    OLED_Clear();
    if(drv_sht20_gettemp() >= 30.00)
    {        
        OLED_ShowPicture(0,0,128,64,BMP2,1);
        OLED_Refresh();
        drv_systick_ms(1000);
        OLED_Clear();        
    }    
}
void drv_oled_hu(void)
{
    int r = 0;            
    r = (int)(drv_sht20_gethum() * 100);
    OLED_ShowFloat(50, 20 , r, 2, 16, 1);	
    OLED_Refresh();
    drv_systick_ms(2500);
    OLED_Clear();
    if(drv_sht20_gethum() >= 60.00)
    {        
        OLED_ShowPicture(0,0,128,64,BMP2,1);
        OLED_Refresh();
        drv_systick_ms(1000);
        OLED_Clear();        
    }    
}
